package cn.dcw.pethome.user.controller;

import cn.dcw.pethome.basic.exception.MyException;
import cn.dcw.pethome.basic.util.AjaxResult;
import cn.dcw.pethome.user.service.IUserService;
import cn.dcw.pethome.user.service.IUserVerifyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping
@Api(tags = "用户注册校验接口",description = "用户注册校验接口")
public class UserVerifyController {

    @Autowired
    private IUserVerifyService service;

    /**
     * 通过前端传递过来的手机号，查询数据库是否注册过
     * @param phone
     * @return
     */
    @PostMapping("/phoneVerify")
    @ApiOperation(value = "用户手机验证",notes = "用户手机验证是否已经注册过")
    public AjaxResult phoneVerify(@RequestBody Map<String,String> map){
        try {
            String phone = map.get("phone");
            service.findOneByPhone(phone);
            return new AjaxResult(true,"该用户为新用户");
        }catch (MyException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统异常***");
        }
    }

    /**
     * 校验验证码是否一致
     * @param map
     * @return
     */
    @PostMapping("/verifycode/sendSmsCode")
    @ApiOperation(value = "校验验证码是否一致",notes = "手机验证码校验")
    public AjaxResult verifycode(@RequestBody Map<String,String> map){
        try {
            //String phone = map.get("phone");
            service.verifycode(map);
            return new AjaxResult(true,"该用户为新用户");
        }catch (MyException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统异常***");
        }
    }

}
